package com.rzcoding.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rzcoding.dao.OrderMapper;
import com.rzcoding.entity.Order;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationHome;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.FileReader;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
    private JSONObject logistics = null;
    private String dirPath = null;


    @PostConstruct
    public void getJsonFromFile() {
        dirPath = new ApplicationHome(getClass()).getSource().getParentFile().toString();
        try (BufferedReader reader = new BufferedReader(new FileReader(dirPath+"/classes/logistic.json"))) {
            String str = IOUtils.toString(reader);
            logistics =  JSON.parseObject(str, JSONObject.class);
        } catch (Exception e) {
            logistics =  new JSONObject();
            e.printStackTrace();
        }
    }

    public PageInfo<Order> findPage(String orderNum, int pageNum, int pageSize){
        Order order = new Order();
        if(org.apache.commons.lang3.StringUtils.isNotBlank(orderNum)){
            order.setOrderNumber(orderNum+"%");
        }
        //分页
        PageHelper.startPage(pageNum,pageSize);
        //搜索条件构建
        Example example = createExample(order);
        //执行搜索
        return new PageInfo<>(orderMapper.selectByExample(example));
    }

    /**
     * Order构建查询对象
     * @param order
     * @return
     */
    public Example createExample(Order order){
        Example example=new Example(Order.class);
        Example.Criteria criteria = example.createCriteria();
        if(order!=null){
            // 主键id
            if(!StringUtils.isEmpty(order.getId())){
                    criteria.andEqualTo("id",order.getId());
            }
            // 订单编号
            if(!StringUtils.isEmpty(order.getOrderNumber())){
                    criteria.andLike("orderNumber",order.getOrderNumber());
            }
            // 订单总金额
            if(!StringUtils.isEmpty(order.getOrderPrice())){
                    criteria.andEqualTo("orderPrice",order.getOrderPrice());
            }
            // 订单是否已经发货
            if(!StringUtils.isEmpty(order.getIsSend())){
                    criteria.andEqualTo("isSend",order.getIsSend());
            }
            // 订单状态： 0未付款、1已付款
            if(!StringUtils.isEmpty(order.getPayStatus())){
                    criteria.andEqualTo("payStatus",order.getPayStatus());
            }
            // 记录生成时间
            if(!StringUtils.isEmpty(order.getCreateTime())){
                    criteria.andEqualTo("createTime",order.getCreateTime());
            }
            // 记录修改时间
            if(!StringUtils.isEmpty(order.getModifyTime())){
                    criteria.andEqualTo("modifyTime",order.getModifyTime());
            }
        }
        return example;
    }


    public JSONArray findLogistics() {
        int num = (int) (Math.random() * 100);
        return logistics.getJSONArray(num+"");
    }

    public String getDirPath() {
        return dirPath;
    }

    public void setDirPath(String dirPath) {
        this.dirPath = dirPath;
    }
}
